Back to course home
0% completed
Vote For New Content
Maximum Score of a Good Subarray (hard)
Problem Statement
Given an array of positive integers nums (0-indexed) and an integer k, return the maximum possible score of a good subarray.
The score of a subarray (i, j) is defined as min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1). If any subarray follows i <= k <= j condition, it is considered as good subarray.
Examples
Example 1:
- Input:
nums = [1, 2, 3, 4, 5], k = 2 - Expected Output:
9 - Justification: The best subarray is
[3, 4, 5], where the minimum value is3, and the length is3. The score is3 * 3 = 9.
Example 2:
- Input:
nums = [8, 1, 2, 4, 3], k = 3 - Expected Output:
6 - Justification: The best subarray is
[2, 4, 3], where the minimum value is2, and the length is3. The score is2 * 3 = 6.
Example 3:
- Input:
nums = [4, 6, 4, 7, 2, 5, 1], k = 4 - Expected Output:
12 - Justification: The optimal subarray is
[4, 6, 4, 7, 2, 5], with the minimum value2and length6. Therefore, the score is2 * 6 = 12.
Try it yourself
Try solving this question here:
Java
Saved to Cloud
. . . .
Solution: Sum of Distances in Tree
Solution: Maximum Score of a Good Subarray
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself